
require('../css/commonCss/normalize.css')
require('../css/commonCss/basic.css')
require('../css/commonCss/property.css')
require('../css/commonCss/reset.css')
require('../liabs/iconfont/iconfont.css')

require('weui')
const weUi = require('weui.js')

require('../css/register.less')
//验证码
const captchaCode = require('captcha-mini')
//axios
const axios = require('axios')

/* 获取元素 */
const telphone = document.querySelector('#telphone')
const pwd = document.querySelector('#pwd')
let code = document.querySelector('.code')
const checkpwd = document.querySelector('#checkpwd')
const regsterbtn = document.querySelector('#regsterbtn')
const errMsg = document.querySelector('#errmsg')



let verificationCode = ''
//文档加载事件
document.addEventListener('DOMContentLoaded', function () {
    let captcha = new captchaCode({
        lineWidth: 1,   //线条宽度
        lineNum: 0,       //线条数量
        dotR: 2,          //点的半径
        dotNum: 25,       //点的数量
        fontSize: 46,           //字体大小
        fontFamily:  ['微软雅黑'],  //字体类型
        fontStyle: 'fill',      //字体绘制方法，有fill和stroke
        content: '一个验证码abcdefghijklmnopqrstuvw生成的插件使用的是canvas显示',  //验证码内容
        length: 4    //验证码长度
    }); 

    captcha.draw(document.querySelector('#captcha'),function(r){
        verificationCode = r;
        console.log(r)
    });

    regsterbtn.addEventListener('click', function () {

        const telphoneVal = telphone.value;
        const pwdVal = pwd.value;
        const checkpwdVal = checkpwd.value;
    
        if (!/^1[3-9][0-9]{9}$/.test(telphoneVal)) {
            errMsg.textContent = '手机号错误';
            return;
        } else if( code.value!= verificationCode){
            errMsg.textContent = '验证码错误';
            return;
        } 
        else if (!/^[0-9]{6}$/.test(pwdVal)) {
            errMsg.textContent = '密码错误'
            return;
        } else if (checkpwdVal != pwdVal) {
            errMsg.textContent = '两次密码不一致'
            return;
        } else {
            errMsg.textContent = ''
            axios.post('http://139.9.177.51:8099/users/add',{
                account:telphoneVal,
                password:pwdVal,
            }).then(function(res){
                if(res.data.status === 0){
                    //弹出轻提示
                    weUi.toast('注册成功',{
                        duration:1000,
                        callback: function(){
                            location.href = './logoin.html'
                        }
                    });
                }else{
                    weUi.confirm(res.data.msg)
                }
            })
        }
    })

})
